package com.ty.service.imple;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ty.dao.BaseDaoI;
import com.ty.entity.SysAdmin;
import com.ty.entity.SysMenus;
import com.ty.entity.SysRole;
import com.ty.entity.SysUserRole;
import com.ty.service.AdminService;
import com.ty.util.Pager;

@Service
public class AdminServiceImple implements AdminService {

	@Autowired
	private BaseDaoI<SysAdmin> dao;

	//角色和用户关系表
	@Autowired
	private BaseDaoI<SysUserRole> userRoleDao;
	@Autowired
	private BaseDaoI<SysMenus> menusDao;
	
	@Override
	public Pager selectByPager(int pageNum, int pageSize, SysAdmin admin) {
		
		//查询条件
		Map<String,Object> params=new HashMap<>();
				
				
		String hql="from SysAdmin o";
		String countHql="select count(o) "+hql;
		
		String where=" where 1=1 ";
		
		if(admin!=null&&admin.getUsername()!=null&&!"".equals(admin.getUsername())) {
			where+=" and o.username=:username";
			params.put("username", admin.getUsername());
		}
		
		hql+=where;
		countHql+=where;
		
		Pager page=new Pager();
		//获取总行数
		page.setTotalCount(dao.count(countHql,params).intValue());
		//获取总页数
		page.setPageSize(pageSize);
		page.setCurrentPageIndex(pageNum);
		//分页查询
		List<SysAdmin> list= dao.find(hql, params, page.getCurrentPageIndex(), page.getPageSize());
		page.setData(list);
		
		return page;
	}

	@Override
	public boolean deleteById(Long id) {
		try {
			SysAdmin admin = getById(id);
			dao.delete(admin);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean updateByObj(SysAdmin sysAdmin) {
		try {
			dao.update(sysAdmin);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public SysAdmin getById(Long id) {
		// TODO Auto-generated method stub
		return dao.get(SysAdmin.class, id);
	}

	@Override
	public boolean deleteBatch(String ids) {
		try {
			String idArrays[] = ids.split(",");
			for (String id : idArrays) {
				this.deleteById(Long.valueOf(id));
			}
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	public boolean saveObject(SysAdmin admin) {
		try {
		   dao.save(admin);
		   return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public SysAdmin getByName(String param) {
		String hql="from SysAdmin o where o.username=:username";
		Map<String,Object> params=new HashMap();
		params.put("username", param);
		List<SysAdmin> list= dao.find(hql, params);
		if(list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public SysAdmin login(String username, String password) {
		String hql="from SysAdmin o where o.username=:username and o.password=:password";
		Map<String,Object> params=new HashMap();
		params.put("username", username);
		params.put("password", password);
		List<SysAdmin> list= dao.find(hql,params);
		if(list!=null&&list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Map<String, Object> selectByUserAuth(SysAdmin sysAdmin) {
		String userRoleHql="from SysUserRole o inner join fetch o.sysRole b   where o.sysAdmin.id="+sysAdmin.getId();
		List<SysUserRole> userRoleList= userRoleDao.find(userRoleHql);
		Map<String, Object> bean=new HashMap<>();
		if(userRoleList==null) { //没有角色
			return bean;
		}
		
		//获取角色中所有菜单编号
		List<String> roleList=new ArrayList<>();
		
		Set<Long> mensSet=new HashSet<>();
		for (SysUserRole sysUserRole : userRoleList) {
			 SysRole sysRole=sysUserRole.getSysRole();
			 roleList.add(sysRole.getCode());
			 String strMenus=sysRole.getMenus();
			 if(strMenus==null) {
				 continue;
			 }
			 String menus[]= strMenus.split(",");
			 for (Object mensId : menus) {
				 mensSet.add(Long.valueOf(mensId.toString()));
			 }
			 
		}
		
		//获取菜单的权限
		String menusHql="select o.code from SysMenus o where o.id in(:menusId) and o.code<>'' and o.code is not null";
		Query query= menusDao.getCurrentSession().createQuery(menusHql);
		query.setParameterList("menusId", mensSet);
		List list= query.list(); //获取所有菜单权限
		
		bean.put("role",roleList);
		bean.put("menus",list);
		return bean;
	}

}
